package com.hskn.hss.module.workrule.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlthresholdtrans.mapper.TlThresholdTransMapper;
import com.hskn.hss.module.user.mapper.UserMapper;
import com.hskn.hss.module.videoplan.entity.VideoPlan;
import com.hskn.hss.module.workrule.entity.WorkRule;
import com.hskn.hss.module.workrule.entity.WorkRuleFull2;
import com.hskn.hss.module.workrule.mapper.WorkRuleMapper;
import com.hskn.hss.module.workrule.service.IWorkRuleService;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-12
 */
@Slf4j
@RestController
@RequestMapping("/hss/workrule")
@Api(tags = {"作业规则"}, description = "作业规则")
public class WorkRuleController {

    @Autowired
    private IWorkRuleService workRuleService;
    @Resource
    private WorkRuleMapper workRuleMapper;

    @SysLog(businessType = BusinessType.UPDATE, tableName = "work_rule", idKey = "workRuleId", bean = WorkRule.class, beanMapper = WorkRuleMapper.class, names = "ruleName")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            workRuleService.mUpdate(params);
            return AjaxResult.success("修改成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> list(HttpServletRequest request) {
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = workRuleService.mList(entity, params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.INSERT, tableName = "work_rule", bean = WorkRule.class, beanMapper = WorkRuleMapper.class, names = "ruleName")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public AjaxResult<Object> add(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            QueryWrapper<WorkRule> wrokRuleQueryWrapper = new QueryWrapper<>();
            wrokRuleQueryWrapper.lambda().eq(WorkRule::getRuleName, params.getString("ruleName"));
            List<WorkRule> workRuleList = workRuleMapper.selectList(wrokRuleQueryWrapper);
            if(workRuleList.isEmpty()){
                workRuleService.add(params);
                return AjaxResult.success("发布成功");
            }else{
                return AjaxResult.fail("该作业规则名称已存在，请重新输入");
            }
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "deleteJxqs", method = RequestMethod.POST)
    public AjaxResult<Object> deleteJxqs(@RequestBody @Validated WorkRule workRule) {
        try {
            return workRuleService.deleteJxqs(workRule);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "deleteLjqy", method = RequestMethod.POST)
    public AjaxResult<Object> deleteLjqy(@RequestBody @Validated WorkRule workRule) {
        try {
            return workRuleService.deleteLjqy(workRule);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "deleteLjzy", method = RequestMethod.POST)
    public AjaxResult<Object> deleteLjzy(@RequestBody @Validated WorkRule workRule) {
        try {
            return workRuleService.deleteLjzy(workRule);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "deleteDlbj", method = RequestMethod.POST)
    public AjaxResult<Object> deleteDlbj(@RequestBody @Validated WorkRule workRule) {
        try {
            return workRuleService.deleteDlbj(workRule);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "detail", method = RequestMethod.POST)
    public AjaxResult<Object> detail(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            WorkRuleFull2 workRuleFull = workRuleService.detail(params);
            return AjaxResult.success(workRuleFull);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

}

